package com.library.management.controller;

import com.library.management.common.PageRequest;
import com.library.management.common.PageResult;
import com.library.management.common.Result;
import com.library.management.entity.Book;
import com.library.management.entity.BookComment;
import com.library.management.entity.User;
import com.library.management.service.BookCommentService;
import com.library.management.service.BookService;
import com.library.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书评论控制器
 */
@Controller
@RequestMapping("/comment")
public class BookCommentController {
    @Autowired
    private BookCommentService bookCommentService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    /**
     * 评论列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<User> users = userService.list(new User(), 1, Integer.MAX_VALUE).getList();
        List<Book> books = bookService.list(new Book(), 1, Integer.MAX_VALUE).getList();
        model.addAttribute("users", users);
        model.addAttribute("books", books);
        return "comment/list";
    }

    /**
     * 评论详情页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        BookComment comment = bookCommentService.getById(id);
        model.addAttribute("comment", comment);
        return "comment/detail";
    }

    /**
     * 添加评论页面
     */
    @GetMapping("/add")
    public String add(Model model) {
        List<User> users = userService.list(new User(), 1, Integer.MAX_VALUE).getList();
        List<Book> books = bookService.list(new Book(), 1, Integer.MAX_VALUE).getList();
        model.addAttribute("users", users);
        model.addAttribute("books", books);
        return "comment/add";
    }

    /**
     * 查询评论列表
     */
    @GetMapping("/api/list")
    @ResponseBody
    public Result<PageResult<BookComment>> list(BookComment comment, PageRequest pageRequest) {
        PageResult<BookComment> pageResult = bookCommentService.list(comment, pageRequest.getPageNum(),
                pageRequest.getPageSize());
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询评论
     */
    @GetMapping("/api/get/{id}")
    @ResponseBody
    public Result<BookComment> get(@PathVariable Long id) {
        BookComment comment = bookCommentService.getById(id);
        return Result.success(comment);
    }

    /**
     * 添加评论
     */
    @PostMapping("/api/add")
    @ResponseBody
    public Result<Integer> add(@RequestBody BookComment comment) {
        int result = bookCommentService.add(comment);
        return Result.success(result);
    }

    /**
     * 更新评论
     */
    @PutMapping("/api/update")
    @ResponseBody
    public Result<Integer> update(@RequestBody BookComment comment) {
        int result = bookCommentService.update(comment);
        return Result.success(result);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/api/delete/{id}")
    @ResponseBody
    public Result<Integer> delete(@PathVariable Long id) {
        int result = bookCommentService.delete(id);
        return Result.success(result);
    }

    /**
     * 查询用户的评论
     */
    @GetMapping("/api/listByUserId/{userId}")
    @ResponseBody
    public Result<List<BookComment>> listByUserId(@PathVariable Long userId) {
        List<BookComment> list = bookCommentService.listByUserId(userId);
        return Result.success(list);
    }

    /**
     * 查询图书的评论
     */
    @GetMapping("/api/listByBookId/{bookId}")
    @ResponseBody
    public Result<List<BookComment>> listByBookId(@PathVariable Long bookId) {
        List<BookComment> list = bookCommentService.listByBookId(bookId);
        return Result.success(list);
    }
}